package com.coolwar.design.proxy.dynamic.feign;

import org.springframework.web.bind.annotation.PostMapping;

import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Coolwar
 * 动态代理
 */
public class ProxyFactory<T> {

    private final Class<T> mapperInterface;

    public ProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance() {
        return (T) Proxy.newProxyInstance(
                mapperInterface.getClassLoader(),
                new Class[]{mapperInterface},
                (proxy, method, args) -> {
                    System.out.println("调用全路径名：" + mapperInterface.getName());
                    System.out.println("调用方法名：" + method.getName());
                    System.out.println("调用方法参数：" + Arrays.toString(args));
                    System.out.println("调用服务名：" + mapperInterface.getAnnotation(FeignClient.class).name());
                    System.out.println("调用url：" + mapperInterface.getAnnotation(FeignClient.class).url());
                    System.out.println("调用请求路径：" + Arrays.toString(method.getDeclaredAnnotation(PostMapping.class).value()));
                    System.out.println("自定义执行器发送http请求：");
                    Map<String, Object> param = new HashMap<>(8);
                    param.put("code", 200);
                    param.put("success", "ok");
                    return param;
                }
        );
    }

}
